﻿///Khoa phim khong cho nhap chu va formmatnumber theo dinh dang chuan
var isShift = false;
var seperator = "/";
function DateFormat(txt, keyCode) {
    if (keyCode == 16)
        isShift = true;
    //Validate that its Numeric
    if (((keyCode >= 48 && keyCode <= 57) || keyCode == 8 || keyCode <= 37 || keyCode <= 39 || (keyCode >= 96 && keyCode <= 105)) && isShift == false) {
        if ((txt.value.length == 2 || txt.value.length == 5) && keyCode != 8) {
            txt.value += seperator;
        }
        return true;
    }
    else {
        return false;
    }
}
function ValidateDate(txt, keyCode, strCulture) {
    if (keyCode == 16)
        isShift = false;
    var val = txt.value;
    if (val.length == 10) {
        var splits = val.split("/");
        ///vi-VN
        if (strCulture == 'vi-VN') {
            var dt = new Date(splits[1] + "/" + splits[0] + "/" + splits[2]);
            //Validation for Dates
            if (dt.getDate() == splits[0] && dt.getMonth() + 1 == splits[1] && dt.getFullYear() == splits[2]) {
                txt.className = "IsValidate";
                txt.title = "OK";
            }
            else {
                txt.className = "WarningInput";
                return;
            }
        }
        else if (strCulture == 'en-US') {
            //en-US
            var dt = new Date(splits[0] + "/" + splits[1] + "/" + splits[2]);
            //Validation for Dates
            if (dt.getDate() == splits[1] && dt.getMonth() + 1 == splits[0] && dt.getFullYear() == splits[2]) {
                txt.className = "IsValidate";
                txt.title = "OK";
            }
            else {
                txt.className = "WarningInput";
                return;
            }
        }
    }
    else if (val.length < 10) {
        txt.className = "WarningInput";
    }
}
function GetDecimalDelimiter(countryCode) {

    switch (countryCode) {
        case 3:
            return '#';
        case 2:
            return ',';
        default:
            return '.';
    }
}
function GetCommaDelimiter(countryCode) {

    switch (countryCode) {
        case 3:
            return '*';
        case 2:
            return ',';
        default:
            return ',';
    }

}
function FormatClean(num) {
    var sVal = '';
    var nVal = num.length;
    var sChar = '';

    try {
        for (c = 0; c < nVal; c++) {
            sChar = num.charAt(c);
            nChar = sChar.charCodeAt(0);
            if ((nChar >= 48) && (nChar <= 57)) { sVal += num.charAt(c); }
        }
    }
    catch (exception) { AlertError("Format Clean", exception); }
    return sVal;
}
function FormatNumber(num, countryCode, decimalPlaces) {

    var minus = '';
    var comma = '';
    var dec = '';
    var preDecimal = '';
    var postDecimal = '';

    try {

        decimalPlaces = parseInt(decimalPlaces);
        comma = GetCommaDelimiter(countryCode);
        dec = GetDecimalDelimiter(countryCode);

        if (decimalPlaces < 1) { dec = ''; }
        if (num.lastIndexOf("-") == 0) { minus = '-'; }

        preDecimal = FormatClean(num);

        // preDecimal doesn't contain a number at all.
        // Return formatted zero representation.

        if (preDecimal.length < 1) {
            return '';
            //return minus + FormatEmptyNumber(dec, decimalPlaces);
        }

        // preDecimal is 0 or a series of 0's.
        // Return formatted zero representation.

        if (parseInt(preDecimal) < 1 && preDecimal.length > decimalPlaces) {
            //return '';
            return minus + FormatEmptyNumber(dec, decimalPlaces);
        }

        // predecimal has no numbers to the left.
        // Return formatted zero representation.

        if (preDecimal.length == decimalPlaces) {
            //return minus + '0' + dec + preDecimal;
            return preDecimal;
        }

        // predecimal has fewer characters than the
        // specified number of decimal places.
        // Return formatted leading zero representation.

        if (preDecimal.length < decimalPlaces) {

            return preDecimal;
            //                    if (decimalPlaces == 2) {
            //                        return minus + FormatEmptyNumber(dec, decimalPlaces - 1) + preDecimal;
            //                    }
            //                    return minus + FormatEmptyNumber(dec, decimalPlaces - 2) + preDecimal;
        }

        // predecimal contains enough characters to
        // qualify to need decimal points rendered.
        // Parse out the pre and post decimal values
        // for future formatting.

        if (preDecimal.length > decimalPlaces) {
            postDecimal = dec + preDecimal.substring(preDecimal.length - decimalPlaces,
                                               preDecimal.length);
            preDecimal = preDecimal.substring(0, preDecimal.length - decimalPlaces);
        }

        // Place comma oriented delimiter every 3 characters
        // against the numeric represenation of the "left" side
        // of the decimal representation.  When finished, return
        // both the left side comma formatted value together with
        // the right side decimal formatted value.

        var regex = new RegExp('(-?[0-9]+)([0-9]{3})');

        while (regex.test(preDecimal)) {
            preDecimal = preDecimal.replace(regex, '$1' + comma + '$2');
        }

    }
    catch (exception) { AlertError("Format Number", exception); }
    return minus + preDecimal + postDecimal;
}

function FormatEmptyNumber(decimalDelimiter, decimalPlaces) {
    var preDecimal = '0';
    var postDecimal = '';

    for (i = 0; i < decimalPlaces; i++) {
        if (i == 0) { postDecimal += decimalDelimiter; }
        postDecimal += '0';
    }

    return preDecimal + postDecimal;
}
function AlertError(methodName, e) {
    if (e.description == null) { alert(methodName + " Exception: " + e.message); }
    else { alert(methodName + " Exception: " + e.description); }
}
///Kho Key tren ban phim chi cho nhap so bao gom ca so am
/// Cach nhap so am: la nhap so roi ve dau them dau "-" vay la OK.
function blockNonNumbers(obj, e, allowDecimal, allowNegative) {
    var key;
    var isCtrl = false;
    var keychar;
    var reg;

    if (window.event) {
        key = e.keyCode;
        isCtrl = window.event.ctrlKey        
    }
    else if (e.which) {
        key = e.which;
        isCtrl = e.ctrlKey;
        obj.className = "WarningInput";        
    }

    if (isNaN(key)) {        
        return true;        
    }    
    keychar = String.fromCharCode(key);

    // check for backspace or delete, or if Ctrl was pressed
    if (key == 8 || isCtrl) {
        obj.className = "IsValidate";
        return true;
    }
    reg = /\d/;
    var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
    var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
    
    if (reg.test(keychar))
        obj.className = "IsValidate";        
    return isFirstN || isFirstD || reg.test(keychar);
}
 